<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="领用物品id" prop="receiveAssetId">-->
<!--        <el-input-->
<!--          v-model="queryParams.receiveAssetId"-->
<!--          placeholder="请输入领用物品id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="物品名称" prop="receiveAssetName">
        <el-input
          v-model="queryParams.receiveAssetName"
          placeholder="请输入领用物品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="物品类别名称" prop="receiveClassName">-->
<!--        <el-input-->
<!--          v-model="queryParams.receiveClassName"-->
<!--          placeholder="请输入物品类别名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="品牌" prop="receiveAssetBrand">-->
<!--        <el-input-->
<!--          v-model="queryParams.receiveAssetBrand"-->
<!--          placeholder="请输入品牌"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="规格型号" prop="receiveAssetModel">-->
<!--        <el-input-->
<!--          v-model="queryParams.receiveAssetModel"-->
<!--          placeholder="请输入规格型号"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="数量" prop="receiveQuantity">-->
<!--        <el-input-->
<!--          v-model="queryParams.receiveQuantity"-->
<!--          placeholder="请输入数量"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="领用时间" prop="getTime">
        <el-date-picker clearable
                        v-model="queryParams.getTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择领用时间">
        </el-date-picker>
      </el-form-item>
<!--      <el-form-item label="领用人id" prop="receiveUserId">-->
<!--        <el-input-->
<!--          v-model="queryParams.receiveUserId"-->
<!--          placeholder="请输入领用人id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="领用人" prop="receiveUserName">
        <el-input
          v-model="queryParams.receiveUserName"
          placeholder="请输入领用人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="领用人部门id" prop="receiveDeptId">-->
<!--        <el-input-->
<!--          v-model="queryParams.receiveDeptId"-->
<!--          placeholder="请输入领用人部门id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="领用人部门名称" prop="receiveDeptName">-->
<!--        <el-input-->
<!--          v-model="queryParams.receiveDeptName"-->
<!--          placeholder="请输入领用人部门名称"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="是否需归还" prop="isNeedReturn">
        <el-input
          v-model="queryParams.isNeedReturn"
          placeholder="请输入是否需归还"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="approval">
        <el-input
          v-model="queryParams.approval"
          placeholder="请输入审核状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否归还" prop="isReturn">
        <el-input
          v-model="queryParams.isReturn"
          placeholder="请输入是否归还"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="归还时间" prop="returnTime">-->
<!--        <el-date-picker clearable-->
<!--                        v-model="queryParams.returnTime"-->
<!--                        type="date"-->
<!--                        value-format="yyyy-MM-dd"-->
<!--                        placeholder="请选择归还时间">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['module:AssetReceive:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['module:AssetReceive:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['module:AssetReceive:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['module:AssetReceive:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="AssetReceiveList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="自增id" align="center" prop="id" />
      <el-table-column label="领用物品id" align="center" prop="receiveAssetId" />
      <el-table-column label="领用物品名称" align="center" prop="receiveAssetName" />
      <el-table-column label="物品类别名称" align="center" prop="receiveClassName" />
      <el-table-column label="品牌" align="center" prop="receiveAssetBrand" />
      <el-table-column label="规格型号" align="center" prop="receiveAssetModel" />
      <el-table-column label="数量" align="center" prop="receiveQuantity" />
      <el-table-column label="领用时间" align="center" prop="getTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.getTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="用途" align="center" prop="receiveUse" />
      <el-table-column label="领用人id" align="center" prop="receiveUserId" />
      <el-table-column label="领用人名称" align="center" prop="receiveUserName" />
      <el-table-column label="领用人部门id" align="center" prop="receiveDeptId" />
      <el-table-column label="领用人部门名称" align="center" prop="receiveDeptName" />
<!--      <el-table-column label="是否需归还" align="center" prop="isNeedReturn" />-->
      <el-table-column label="审核状态" align="center" prop="approval" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="是否归还" align="center" prop="isReturn" />
      <el-table-column label="归还时间" align="center" prop="returnTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.returnTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="openReturn(scope.row)"
            v-hasPermi="['module:AssetReceive:AssetIsReturn']"
          >归还</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="openRepair(scope.row)"
            v-hasPermi="['module:AssetReceive:updateIsRepair']"
          >报修</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['module:AssetReceive:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['module:AssetReceive:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改资产领用对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
<!--        <el-form-item label="领用物品id" prop="receiveAssetId">-->
<!--          <el-input v-model="form.receiveAssetId" placeholder="请输入领用物品id" />-->
<!--        </el-form-item>-->

        <el-form-item label="领用物品" prop="receiveAssetName">
          <el-input style="width: 250px" v-model="form.receiveAssetName" readonly placeholder="请输入领用物品">
            <template slot="append">
              <el-button type="primary" @click="openDetails">
                选择领用物品
              </el-button>
            </template>
          </el-input>
        </el-form-item>

<!--        <el-form-item label="领用物品" prop="receiveAssetName">-->
<!--          <el-input v-model="form.receiveAssetName" placeholder="请输入领用物品名称" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="物品类别名称" prop="receiveClassName">-->
<!--          <el-input v-model="form.receiveClassName" placeholder="请输入物品类别名称" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="品牌" prop="receiveAssetBrand">-->
<!--          <el-input v-model="form.receiveAssetBrand" placeholder="请输入品牌" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="规格型号" prop="receiveAssetModel">-->
<!--          <el-input v-model="form.receiveAssetModel" placeholder="请输入规格型号" />-->
<!--        </el-form-item>-->
        <el-form-item label="数量" prop="receiveQuantity">
          <el-input v-model="form.receiveQuantity" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="领用时间" prop="getTime">
          <el-date-picker clearable
                          v-model="form.getTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择领用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="用途" prop="receiveUse">
          <el-input v-model="form.receiveUse" type="textarea" placeholder="请输入内容" />
        </el-form-item>
<!--        <el-form-item label="领用人id" prop="receiveUserId">-->
<!--          <el-input v-model="form.receiveUserId" placeholder="请输入领用人id" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="领用人名称" prop="receiveUserName">-->
<!--          <el-input v-model="form.receiveUserName" placeholder="请输入领用人名称" />-->
<!--        </el-form-item>-->
          <el-form-item label="领用人" prop="receiveUserName">
            <el-input style="width: 250px" v-model="form.receiveUserName" readonly placeholder="请输入领用人名称">
              <template slot="append">
                <el-button type="primary" @click="openImportUser()">
                  选择领用人
                </el-button>
              </template>
            </el-input>
          </el-form-item>

<!--        <el-form-item label="领用人部门id" prop="receiveDeptId">-->
<!--          <el-input v-model="form.receiveDeptId" placeholder="请输入领用人部门id" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="领用人部门名称" prop="receiveDeptName">-->
<!--          <el-input v-model="form.receiveDeptName" placeholder="请输入领用人部门名称" />-->
<!--        </el-form-item>-->

<!--        <el-form-item label="是否需归还" prop="isNeedReturn">-->
<!--          <el-input v-model="form.isNeedReturn" placeholder="请输入是否需归还" />-->
<!--        </el-form-item>-->

<!--        <el-form-item label="是否需归还" prop="isNeedReturn">-->
<!--          <el-select v-model="form.isNeedReturn" placeholder="是否需归还">-->
<!--            <el-option-->
<!--              v-for="item in options"-->
<!--              :key="item.value"-->
<!--              :label="item.label"-->
<!--              :value="item.value"-->
<!--            ></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->

<!--        <el-form-item label="审核状态" prop="approval">-->
<!--          <el-input v-model="form.approval" placeholder="请输入审核状态" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="0存在，1删除" prop="delFlag">-->
<!--          <el-input v-model="form.delFlag" placeholder="请输入0存在，1删除" />-->
<!--        </el-form-item>-->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
<!--        <el-form-item label="是否归还" prop="isReturn">-->
<!--        <el-input readonly v-model="form.isReturn" placeholder="请输入是否归还" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="归还时间" prop="returnTime">-->
<!--          <el-date-picker readonly clearable-->
<!--                          v-model="form.returnTime"-->
<!--                          type="date"-->
<!--                          value-format="yyyy-MM-dd"-->
<!--                          placeholder="请选择归还时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <import-user ref="import" @showUser="showUser"></import-user>
    <details-num ref="defs" @showDetails="showDetails"></details-num>

  </div>
</template>

<script>
  import { listAssetReceive, getAssetReceive, delAssetReceive, addAssetReceive, updateAssetReceive,AssetIsReturn,updateIsRepair } from "@/api/module/AssetReceive";
  import importUser from "../customer/user";
  import { getDicts } from '@/api/system/dict/data';
  import detailsNum from "@/views/module/AssetReceive/details"
  export default {
    name: "AssetReceive",
    components: {importUser,detailsNum},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 资产领用表格数据
        AssetReceiveList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          receiveAssetId: null,
          receiveAssetName: null,
          receiveClassName: null,
          receiveAssetBrand: null,
          receiveAssetModel: null,
          receiveQuantity: null,
          getTime: null,
          receiveUse: null,
          receiveUserId: null,
          receiveUserName: null,
          receiveDeptId: null,
          receiveDeptName: null,
          isNeedReturn: null,
          approval: null,
          isReturn: null,
          returnTime: null,

        },
        classNameOptions:[],
        options: [{
          value: '是',
          label: '是'
        },
          {
            value: '否',
            label: '否'
          },
        ],

        // 表单参数
        form: {},
        // 表单校验
        rules: {
          receiveAssetName:[{required:true,message:'请选择领用物品',trigger:['blur','change']}],
          receiveQuantity:[{required:true,message:'请输入数量',trigger:['blur','change']}],
          getTime:[{required:true,message:'请选择时间',trigger:['blur','change']}],
          receiveUserName:[{required:true,message:'请选择领用人',trigger:['blur','change']}],
        }
      };
    },
    created() {
      this.getList();
      this.getAsssetClass();
    },
    methods: {
      /** 查询资产领用列表 */
      getList() {
        this.loading = true;
        listAssetReceive(this.queryParams).then(response => {
          this.AssetReceiveList = response.rows;
          // 资产类型判断
          let arr = this.AssetReceiveList;
          arr.forEach(item => {
            item.receiveClassName = this.getValue(this.classNameOptions, item.receiveClassName)
          });
          this.AssetReceiveList = arr;
          this.total = response.total;
          this.loading = false;
        });
      },
      getValue(arr, item) {
        for (let i = 0; i < arr.length; i++) {
          if (item === arr[i].dictValue) {
            return arr[i].dictLabel
          }
        }
      },
      // 获取资产类别
      getAsssetClass() {
        getDicts('asset_class').then(response => {
          this.classNameOptions = response.data
        })
        console.log(this.classNameOptions)
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          receiveAssetId: null,
          receiveAssetName: null,
          receiveClassName: null,
          receiveAssetBrand: null,
          receiveAssetModel: null,
          receiveQuantity: null,
          getTime: null,
          receiveUse: null,
          receiveUserId: null,
          receiveUserName: null,
          receiveDeptId: null,
          receiveDeptName: null,
          isNeedReturn: null,
          approval: null,
          createTime: null,
          createBy: null,
          updateTime: null,
          updateBy: null,
          delFlag: null,
          remark: null,
          isReturn: null,
          returnTime: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加资产领用";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const id = row.id || this.ids
        getAssetReceive(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改资产领用";
        });
      },

      /** 归还按钮操作 */
      openReturn(val) {
        this.$confirm('确认归还？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
         this.getReturn(val)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消归还'
          });
        });
      },
     async getReturn(val){
      let res=await AssetIsReturn(val)
       if(res.code == 200){
         this.getList();
         this.$message({
           type: 'success',
           message: '归还成功!'
         });
       }
    },

      /** 报修按钮操作 */
      openRepair(val) {
        this.$confirm('确认报修？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.getRepair(val)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消报修'
          });
        });
      },
      async getRepair(val){
        let res=await updateIsRepair(val)
        if(res.code == 200){
          this.getList();
          this.$message({
            type: 'success',
            message: '报修成功!'
          });
        }
      },


      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateAssetReceive(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addAssetReceive(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$modal.confirm('是否确认删除资产领用编号为"' + ids + '"的数据项？').then(function() {
          return delAssetReceive(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('module/AssetReceive/export', {
          ...this.queryParams
        }, `AssetReceive_${new Date().getTime()}.xlsx`)
      },
      /** 打开用户弹窗 */
      openImportUser() {
        this.$refs.import.show();
      },
      showUser(data) {
        this.form.receiveUserId = data.userId;
        this.form.receiveUserName = data.userName;
        console.log(this.form.receiveUserName);
      },

      /** 打开资产弹窗 */
      openDetails() {
        // this.$refs.defs.dialShow=true
        this.$refs.defs.show()
      },

      showDetails(data){
        console.log(data)
        this.form.receiveAssetId=data.id;
        this.form.receiveAssetName=data.assetName;
        this.form.receiveClassName=data.className;
        this.form.receiveAssetBrand=data.assetBrand;
        this.form.receiveAssetModel=data.assetModel;
      }

    }
  };
</script>
